import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author wonghiu
 * @Date 2021/6/14 17:12
 * @Version 1.0.1
 */
public class Check {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

//        arraylistCheck();
        check();
//        String s = "123";
//        String ss = new String("123");
////        System.out.println(s.equals(ss));
////        System.out.println(s.charAt(0));
//
//        ArrayList<String> strings = new ArrayList<>(11);
//        for (int i = 0; i < 13; i++) {
//            strings.add("11");
//        }
//
//        System.out.println(strings);
//        Class clazz = strings.getClass();
//        Field elementData = clazz.getDeclaredField("elementData");
//        elementData.setAccessible(true);
//        Object[] o = (Object[]) elementData.get(strings);
//
//        System.out.println(o.length);
//        System.out.println(strings.);
//        System.out.println(findLeftMost(new int[]{1,2,3,3,4,5,6},3));
//        System.out.println(findRightMost(new int[]{1,2,3,3,4,5,6},3));
    }

    public static void arraylistCheck() throws NoSuchFieldException, IllegalAccessException {
        System.out.println("case 1: ");
        ArrayList<String> arr = new ArrayList<>();
        System.out.println("初始化后：" + getCap(arr));
        arr.add("11");
        System.out.println("add 1次后：" + getCap(arr));
        for (int i = 0; i < 13; i++) {
            arr.add("11");
        }
        System.out.println("add 13次后：" + getCap(arr));
        System.out.println("-----------------------------");
        System.out.println("case 2: ");
        ArrayList<String> arr2 = new ArrayList<>(12);
        System.out.println("初始化后：" + getCap(arr2));
        for (int i = 0; i < 13; i++) {
            arr2.add("11");
        }
        System.out.println("add了13次后：" + getCap(arr2));
    }

    public static void check() {


        new Thread(() -> {
            long start1 = System.nanoTime();
            noPram();
            long end1 = System.nanoTime();
            System.out.println("noPram: "+(end1-start1));
        }, "1").start();

        new Thread(() -> {
            long start1 = System.nanoTime();
            hasPram();
            long end1 = System.nanoTime();
            System.out.println("hasPram: "+(end1-start1));
        }, "1").start();
    }

    public static void noPram() {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 100000000; i++) {
            strings.add("111");
        }
    }

    public static void hasPram() {
        ArrayList<String> strings = new ArrayList<>(100000000);
        for (int i = 0; i < 100000000; i++) {
            strings.add("111");
        }
    }

    public static int getCap(ArrayList<String> arrayList) throws NoSuchFieldException, IllegalAccessException {
        Class clazz = arrayList.getClass();
        Field elementData = clazz.getDeclaredField("elementData");
        elementData.setAccessible(true);
        Object[] o = (Object[]) elementData.get(arrayList);
        return o.length;
    }

    public static int findLeftMost(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;

        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] >= target) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    public static int findRightMost(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;

        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] <= target) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return r;
    }
}
